package com.learn.xueer.ui.activity

import android.os.Bundle
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.learn.xueer.R
import com.learn.xueer.app.base.BaseActivity
import com.learn.xueer.databinding.ActivityForgetBinding
import com.learn.xueer.ui.viewmodel.RequestLoginViewModel
import kotlinx.android.synthetic.main.activity_forget.et_mobile_code
import kotlinx.android.synthetic.main.activity_forget.tv_submit
import kotlinx.android.synthetic.main.activity_login.et_account
import kotlinx.android.synthetic.main.activity_login.et_code
import kotlinx.android.synthetic.main.activity_login.et_pass
import kotlinx.android.synthetic.main.activity_login.iv_captcha
import me.hgj.jetpackmvvm.ext.view.afterTextChange
import me.hgj.jetpackmvvm.util.ActivityMessenger
import me.hgj.jetpackmvvm.util.get

/**
 * 忘记密码
 */
class ForgetActivity : BaseActivity<RequestLoginViewModel, ActivityForgetBinding>() {
    var from = ""
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.click = ProxyClick()
        mViewModel.captcha()
        mDatabind.viewmodel = mViewModel
        from = intent.get<String>("from").toString()


        et_account.afterTextChange {
            if (it.isEmpty()
                || et_code.text.toString().isEmpty()
                || et_pass.text.toString().isEmpty()
                || et_mobile_code.text.toString().isEmpty()
            ) {
                loginUnable()
            } else {
                loginAble()
            }
        }
        et_pass.afterTextChange {
            if (it.isEmpty() || et_account.text.toString().isEmpty()
                || et_code.text.toString().isEmpty()
                || et_mobile_code.text.toString().isEmpty()
            ) {
                loginUnable()
            } else {
                loginAble()
            }
        }
        et_code.afterTextChange {
            if (it.isEmpty() || et_account.text.toString().isEmpty()
                || et_pass.text.toString().isEmpty()
                || et_mobile_code.text.toString().isEmpty()
            ) {
                loginUnable()
            } else {
                loginAble()
            }
        }
        et_mobile_code.afterTextChange {
            if (it.isEmpty() || et_account.text.toString().isEmpty()
                || et_pass.text.toString().isEmpty()
                || et_code.text.toString().isEmpty()
            ) {
                loginUnable()
            } else {
                loginAble()
            }
        }

    }

    private fun loginAble() {
        tv_submit.setTextColor(resources?.getColor(R.color.white) ?: 0)
        tv_submit.background = resources?.getDrawable(R.drawable.gra_29f19c)
    }

    private fun loginUnable() {
        tv_submit.setTextColor(resources?.getColor(R.color.b5b5b5) ?: 0)
        tv_submit.background = resources?.getDrawable(R.drawable.rec_e5e5e5)
    }

    override fun createObserver() {
        mViewModel.captchaData.observe(this@ForgetActivity, Observer {
            dismissLoading()
            Glide.with(this).load(it).into(iv_captcha)
        })
    }

    inner class ProxyClick {

        fun back() {
            finish()
        }

        fun getmsg() {
            mViewModel.sendSmsReq("resetpwd")
        }

        fun reset() {
            mViewModel.captcha()
        }

        fun submit() {
            if (mViewModel.username.get().isEmpty() || mViewModel.password.get()
                    .isEmpty()
                || mViewModel.capture.value?.isEmpty() == true
                || mViewModel.captureMobile.value?.isEmpty() == true
            ) {
                ToastUtils.showShort("请输入")
                return
            }
            mViewModel.resetPassReq {
                if (from == "person") {
                    ActivityMessenger.finish(this@ForgetActivity, "data" to "login")
                } else {
                    finish()
                }
            }
        }


    }
}